<?php
/**
 * Created by PhpStorm.
 * User: 雨寒
 * Date: 2018/8/24
 * Time: 上午8:04
 */

namespace app\api\module;

use app\common\model\DeveloperException;
use app\common\model\Club;
use app\common\model\User;
use app\common\model\Message;
use app\common\model\UserException;
use think\Db;
class Connection extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
            return true;
    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return string
     */
    public function exec($interface)
    {
        switch ($interface)
        {
            /**
             * @api {post} / 【1001】人脉首页
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName connection_home
             * @apiGroup 人脉【Connection】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Connection",
             *   "interface": "1001",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_code": "0000",
             *   "result_info": "首页",
             *   "data": {
             *     "bussiness_num":"1000",
             *     "news_list":[
             *         {
             *             "id":"1",
             *             "title":"新闻标题",
             *             "content":"xinwenneiroing",
             *             "image":"https://www.baidu.com",
             *             "view_num":"1001",
             *             "tag_id":"2"
             *         },
             *        {
             *             "id":"2",
             *             "title":"新闻标题",
             *             "content":"xinwenneiroing",
             *             "image":"https://www.baidu.com",
             *             "view_num":"1001",
             *             "tag_id":"2"
             *         }
             *     ],
             *     "recommend_connection":[
             *         {
             *             "id":"1",
             *             "avatar":"https://www.baidu.com",
             *         },
             *        {
             *             "id":"2",
             *             "avatar":"https://www.baidu.com",
             *         }
             *     ],
             *     "banner_list":[
             *         {
             *             "id":"1",
             *             "title":"轮播图标题",
             *             "image":"https://www.baidu.com",
             *             "url":""
             *         },
             *        {
             *             "id":"1",
             *             "title":"轮播图标题",
             *             "image":"https://www.baidu.com",
             *             "url":""
             *         }
             *     ],
             *     "bussiness_list":[
             *         {
             *             "id":"1",
             *             "title":"商会名称",
             *             "avatar":[
             *                         {
             *                             "image":"dljfdljfdl"
             *                          },
             *                          {
             *                             "image":"dljfdljfdl"
             *                          }
             *                      ]
             *         },
             *         {
             *             "id":"2",
             *             "title":"商会名称",
             *             "avatar":[
             *                         {
             *                             "image":"dljfdljfdl"
             *                          },
             *                          {
             *                             "image":"dljfdljfdl"
             *                          }
             *                      ]
             *         }
             *     ],
             *     "recommend_bussiness":[
             *         {
             *             "id":"1",
             *             "name":"商会名称",
             *             "manager":"会长",
             *             "image":"https://www.baidu.com",
             *             "member_num":"1001",
             *             "mobile":"18854485469",
             *             "address":"akhfkdihaf",
             *             "is_follow":""
             *         },
             *         {
             *             "id":"1",
             *             "name":"商会名称",
             *             "manager":"会长",
             *             "image":"https://www.baidu.com",
             *             "member_num":"1001",
             *             "mobile":"18854485469",
             *             "address":"akhfkdihaf",
             *             "is_follow":""
             *         }
             *     ],
             *    }
             * }
             */
            case H_CONNECTION_HOME:
                return $this->connection_home();
            /**
             * @api {post} / 【1002】更多人脉
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName connection_member_list
             * @apiGroup 人脉【Connection】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token token
             * @apiParam (dataGroup) {String} industry_id 行业id
             * @apiParam (dataGroup) {String} page 第几页
             * @apiParam (dataGroup) {String} page_size 每页几个
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Connection",
             *   "interface": "1002",
             *   "data": {
             *     "access_token": "6a204bd89f3c8348afd5c77c717a097a",
             *     "page": "1",
             *      "page_size": "3"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {Array} member_list 会员列表
             * @apiSuccess (member_list-回执报文信息) {String} avatar 会员头像
             * @apiSuccess (member_list-回执报文信息) {String} name 姓名
             * @apiSuccess (member_list-回执报文信息) {String} position 哪个部门
             * @apiSuccess (member_list-回执报文信息) {String} mobile 手机号
             * @apiSuccess (member_list-回执报文信息) {String} is_follow 是否关注
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "member_list":[
             *         {
             *                 "avatar":"http://12121",
             *                 "name":"yuhan",
             *                 "position":"技术部",
             *                 "mobile":"18853926512",
             *                 "is_follow":"1"
             *         },
             *         {
             *                 "avatar":"http://12121",
             *                 "name":"yuhan",
             *                 "position":"办公室",
             *                 "mobile":"1888555451",
             *                 "is_follow":"1"
             *         },
             *      ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "更多推荐！"
             * }
             *
             */
            case H_CONNECTION_LIST:
                return $this->connection_list();
            /**
             * @api {post} / 【1003】会员列表(暂不用)
             * @apiDescription <label class="label label-success" style="font-size: 18px;">暂不用</label>
             *
             * @apiVersion 1.0.0
             * @apiName bussiness_member_list
             * @apiGroup 人脉【Connection】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token token
             * @apiParam (dataGroup) {String} bussiness_id 商会id
             * @apiParam (dataGroup) {String} page 第几页
             * @apiParam (dataGroup) {String} page_size 每页几个
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Connection",
             *   "interface": "1003",
             *   "data": {
             *     "access_token": "6a204bd89f3c8348afd5c77c717a097a",
             *     "page": "1",
             *      "page_size": "3",
             *     "bussiness_id": "1"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {Array} member_list 会员列表
             * @apiSuccess (member_list-回执报文信息) {String} avatar 会员头像
             * @apiSuccess (member_list-回执报文信息) {String} name 姓名
             * @apiSuccess (member_list-回执报文信息) {String} position 哪个部门
             * @apiSuccess (member_list-回执报文信息) {String} mobile 手机号
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "member_list":[
             *         {
             *                 "avatar":"http://12121",
             *                 "name":"yuhan",
             *                 "position":"技术部",
             *                 "mobile":"18853926512",
             *         },
             *         {
             *                 "avatar":"http://12121",
             *                 "name":"yuhan",
             *                 "position":"会长",
             *                 "mobile":"1888555451",
             *         },
             *      ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "会员列表！"
             * }
             *
             */
            case H_CONNECTION_MEMBER_LIST:
                return $this->member_list();
            /**
             * @api {post} / 【1004】会员详情(暂不用)
             * @apiDescription <label class="label label-success" style="font-size: 18px;">暂不用</label>
             *
             * @apiVersion 1.0.0
             * @apiName bussiness_member_info
             * @apiGroup 人脉【Connection】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token token
             * @apiParam (dataGroup) {String} uid 用户id
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Connection",
             *   "interface": "1004",
             *   "data": {
             *     "access_token": "13355064521",
             *     "uid": "123456"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data":
             *    {
             *         "avatar":"touxiang",
             *         "name":"姓名",
             *         "position":"商会职位",
             *         "extra":"自己公司职位",
             *         "mobile":"18265156536",
             *         "email":"邮箱",
             *         "branch":"部门",
             *         "service":"服务内容",
             *
             *    }
             *   "result_code": "0000",
             *   "result_info": "会员详情！"
             * }
             *
             */
            case H_CONNECTION_MEMBER_INFO:
                return $this->member_info();
            /**
             * @api {post} / 【1005】关注
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName bussiness_follow
             * @apiGroup 人脉【Connection】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token token
             * @apiParam (dataGroup) {Array} follow_id 被关注id（多个id一键关注）
             * @apiParam (dataGroup) {String} type 关注类型（1：关注会员，2：关注商会）
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Connection",
             *   "interface": "1005",
             *   "data": {
             *     "access_token": "13355064521",
             *     "follow_id": [
             *          "1",
             *          "10"
             *      ],
             *     "type":"1"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data":
             *    {
             *    }
             *   "result_code": "0000",
             *   "result_info": "关注成功！"
             * }
             *
             */
            case H_CONNECTION_BUSSINESS_FOLLOW:
                return $this->bussiness_follow();
            /**
             * @api {post} / 【1006】发掘人脉
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName bussiness_excavate
             * @apiGroup 人脉【Connection】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token token
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Connection",
             *   "interface": "1006",
             *   "data": {
             *     "access_token": "13355064521"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data":
             *    {
             *         "list":[
             *         {
             *             "id":"1",
             *             "avatar":"https://www.baidu.com",
             *         },
             *        {
             *             "id":"2",
             *             "avatar":"https://www.baidu.com",
             *         }
             *     ],
             *    }
             *   "result_code": "0000",
             *   "result_info": "发掘人脉！"
             * }
             *
             */
            case H_CONNECTION_EXCAVATE:
                return $this->connection_excavate();
            /**
             * @api {post} / 【1007】我的人脉
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName my_connection
             * @apiGroup 人脉【Connection】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token token
             * @apiParam (dataGroup) {String} page 第几页
             * @apiParam (dataGroup) {String} page_size 每页几个
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Connection",
             *   "interface": "1007",
             *   "data": {
             *     "access_token": "6a204bd89f3c8348afd5c77c717a097a",
             *     "page": "1",
             *      "page_size": "3"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {Array} member_list 会员列表
             * @apiSuccess (member_list-回执报文信息) {String} avatar 会员头像
             * @apiSuccess (member_list-回执报文信息) {String} name 姓名
             * @apiSuccess (member_list-回执报文信息) {String} position 哪个部门
             * @apiSuccess (member_list-回执报文信息) {String} mobile 手机号
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "member_list":[
             *         {
             *                 "avatar":"http://12121",
             *                 "name":"yuhan",
             *                 "position":"技术部",
             *                 "mobile":"18853926512"
             *         },
             *         {
             *                 "avatar":"http://12121",
             *                 "name":"yuhan",
             *                 "position":"办公室",
             *                 "mobile":"1888555451"
             *         },
             *      ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "我的人脉！"
             * }
             *
             */
            case H_MY_CONNECTION:
                return $this->my_connection();

            default:
                send_developer_error("模块【人脉】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }

    /*人脉首页*/
    public function connection_home()
    {
        /*获取token*/
        $access_token = $this->essentialParam("access_token");

        /*验证token，获取用户信息*/
        $userinfo=User::userFromToken($access_token);

        if(empty($userinfo['bussiness_id']))
        {
            throw new DeveloperException("您还没有所属商会！");
        }

        /*获取商会信息*/
        $bussiness_info=Club::bussinessInfoById($userinfo['bussiness_id']);

        /*默认地区-临沂*/
        if(empty($bussiness_info))
        {
            $bussiness_info['city']=182;
        }

        /*商会会员数量*/
        $member_num = Db::name('user')
            ->where('in_blacklist',0)
            ->where('duties_id',2)
            ->where('bussiness_id',$userinfo['bussiness_id'])
            ->count();

        /*新闻列表*/
        $news_list=Message::newsList($userinfo['bussiness_id'],$bussiness_info['city'],1,3);

        /*轮播图*/
        $banner_list=Message::bannerList(2,$bussiness_info['city']);
        /*商会轮播列表*/
        $bussiness_list=Club::bussinessList(1,1,3,$userinfo);
        /*商会推荐列表*/
        $recommend_bussiness=Club::bussinessList(2,1,3,$userinfo);

        /*推荐的人脉*/
        $recommend_connection=Club::recommendConnection($userinfo,1,6);

        $this->insertResponse('member_num',$member_num);
        $this->insertResponse('news_list',$news_list);
        $this->insertResponse('banner_list',$banner_list);
        $this->insertResponse('bussiness_list',$bussiness_list);
        $this->insertResponse('recommend_bussiness',$recommend_bussiness);
        $this->insertResponse('recommend_connection',$recommend_connection);
        $this->insertResponse('bussiness_id',$userinfo['bussiness_id']);
        $this->insertResponse('bussiness_avatar',$bussiness_info['avatar']);
        $this->insertResponse('bussiness_title',$bussiness_info['title']);


        return $this->response("人脉首页！");
    }

    /*更多人脉暂不用*/
    public function connection_list()
    {
        $access_token=$this->essentialParam('access_token');
        $page=$this->essentialParam('page');
        $page_size=$this->essentialParam('page_size');

        $user=User::userFromToken($access_token);

        /*推荐列表*/
        $member_list=Club::recommendConnection($user,$page,$page_size);
        $this->insertResponse('member_list',$member_list);
        return $this->response('更多人脉');

    }

    /*关注*/
    public function bussiness_follow()
    {
        $access_token=$this->essentialParam("access_token");
        $follow_id=$this->essentialParam("follow_id");
        $type=$this->essentialParam("type");

        $user=User::userFromToken($access_token);

        Club::bussinessFollow($user,$follow_id,$type);

        return $this->response("关注成功！");
    }

    /*发掘人脉*/
    public function connection_excavate()
    {
        $access_token=$this->essentialParam('access_token');

        $user=User::userFromToken($access_token);

        /*发掘列表*/
        $member_list=Club::recommendConnection($user,1,6);
        $this->insertResponse('member_list',$member_list);
        return $this->response('发掘的人脉');

    }

    /*我的人脉*/
    public function my_connection()
    {
        $access_token=$this->essentialParam('access_token');
        $page=$this->essentialParam('page');
        $page_size=$this->essentialParam('page_size');

        $user=User::userFromToken($access_token);
        $list=Club::myConnection($user,$page,$page_size);
        $this->insertResponse('my_connection',$list);
        return $this->response('我的人脉！');
    }

}